#!/bin/sh
#
# see: dh_installdeb(1)

set -e

package="bb-node-red-installer"
service="nodered"

node_red_user="node-red"
node_red_data_dir="/var/lib/node-red"

# Create node-red user with home in /var/lib
echo "Creating/updating ${node_red_user} user account..."
adduser --system --home /var/lib/node-red --gecos "${node_red_user} user" --shell /bin/sh \
	--quiet --disabled-password --group ${node_red_user} || {
echo "Proceeding with existing ${node_red_user} user..."
  }

# Give node_red_user ownership of node_red_data_dir
chown ${node_red_user} ${node_red_data_dir}

main_install () {
	if [ -d /etc/avahi/services/ ] ; then
		#Annouce http server via DNS Sevice Discovery
		wfile="/etc/avahi/services/nodered.service"
		echo "<?xml version=\"1.0\" standalone='no'?><!--*-nxml-*-->" > ${wfile}
		echo "<!DOCTYPE service-group SYSTEM \"avahi-service.dtd\">" >> ${wfile}
		echo "" >> ${wfile}
		echo "<!-- See avahi.service(5) for more information about this configuration file -->" >> ${wfile}
		echo "" >> ${wfile}
		echo "<service-group>" >> ${wfile}
		echo "" >> ${wfile}
		echo "  <name replace-wildcards=\"yes\">Node-RED for %h</name>" >> ${wfile}
		echo "  <service>" >> ${wfile}
		echo "" >> ${wfile}
		echo "    <type>_http._tcp</type>" >> ${wfile}
		echo "    <port>1880</port>" >> ${wfile}
		echo "  </service>" >> ${wfile}
		echo "" >> ${wfile}
		echo "</service-group>" >> ${wfile}
		chown -R root:root ${wfile}
	fi

	systemctl enable ${service}.socket || true
	systemctl start ${service} || true
	echo "${package}:Installed"
}

cleanup () {
	if [ -d /usr/local/lib/node_modules/node-red/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-contrib-blockly/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-contrib-blockly/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-contrib-gpio/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-contrib-gpio/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-contrib-mjpgcamera/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-contrib-mjpgcamera/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-contrib-particle/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-contrib-particle/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-dashboard/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-dashboard/ || true
	fi

	if [ -d /usr/local/lib/node_modules/node-red-node-serialport/ ] ; then
		rm -rf /usr/local/lib/node_modules/node-red-node-serialport/ || true
	fi

	if [ -d /var/cache/bb-node-red-installer/ ] ; then
		rm -rf /var/cache/bb-node-red-installer/ || true
	fi

	if [ -d /root/.node-red/ ] ; then
		rm -rf /root/.node-red/ || true
	fi
}

case "$1" in
    configure)
	main_install
	cleanup
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

